package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysManager;
import com.ruoyi.system.service.ISysManagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统管理员Controller
 * 
 * @author ruoyi
 * @date 2021-09-18
 */
@RestController
@RequestMapping("/system/manager")
public class SysManagerController extends BaseController
{
    @Autowired
    private ISysManagerService sysManagerService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询系统管理员列表
     */
    @PreAuthorize("@ss.hasPermi('system:manager:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysManager sysManager)
    {
        startPage();
        List<SysManager> list = sysManagerService.selectSysManagerList(sysManager);
        return getDataTable(list);
    }

    /**
     * 导出系统管理员列表
     */
    @PreAuthorize("@ss.hasPermi('system:manager:export')")
    @Log(title = "系统管理员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysManager sysManager)
    {
        List<SysManager> list = sysManagerService.selectSysManagerList(sysManager);
        ExcelUtil<SysManager> util = new ExcelUtil<SysManager>(SysManager.class);
        return util.exportExcel(list, "系统管理员数据");
    }

    /**
     * 获取系统管理员详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:manager:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(sysManagerService.selectSysManagerById(userId));
    }

    /**
     * 新增系统管理员
     */
    @PreAuthorize("@ss.hasPermi('system:manager:add')")
    @Log(title = "系统管理员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysManager sysManager)
    {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sysManager.getUserName())))
        {
            return AjaxResult.error("新增管理员'" + sysManager.getUserName() + "'失败，登录账号已存在");
        }
        /*else if (StringUtils.isNotEmpty(sysManager.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(sysManager)))
        {
            return AjaxResult.error("新增管理员'" + sysManager.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(sysManager.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(sysManager)))
        {
            return AjaxResult.error("新增管理员'" + sysManager.getUserName() + "'失败，邮箱账号已存在");
        }*/
        sysManager.setCreateBy(SecurityUtils.getUsername());
        sysManager.setPassword(SecurityUtils.encryptPassword(sysManager.getPassword()));
        sysManager.setUserType(2);
        return toAjax(sysManagerService.insertSysManager(sysManager));
    }

    /**
     * 修改系统管理员
     */
    @PreAuthorize("@ss.hasPermi('system:manager:edit')")
    @Log(title = "系统管理员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysManager sysManager)
    {
        userService.checkUserAllowed(sysManager);
        return toAjax(sysManagerService.updateSysManager(sysManager));
    }

    /**
     * 删除系统管理员
     */
    @PreAuthorize("@ss.hasPermi('system:manager:remove')")
    @Log(title = "系统管理员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(sysManagerService.deleteSysManagerByIds(userIds));
    }
}
